package smlab.view.state
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import smlab.interfaces.IContainer_SM;

	/**
	 * 场景容器/切换器
	 * @author simon
	 *
	 */
	public class StateContainer_SM extends Container_SM
	{
		public const STATE_CHANGED_EVENT:String="state_changed_event";
		/**
		 * 场景数组
		 */
		private var _stateArr:Array;

		/**
		 * 场景名称数组
		 */
		private var _stateNameArr:Array;

		/**
		 * 场景字典
		 */
		private var _stateDict:Dictionary;

		/**
		 * 当前场景
		 */
		private var _currentState:IContainer_SM;

		public function StateContainer_SM()
		{
			super();
			initialize();
		}

		override public function initialize():void
		{
			_stateArr=[];
			_stateNameArr=[];
			_stateDict=new Dictionary;
		}

		/**
		 * 跳转到某场景
		 * @param $stateName
		 *
		 */
		public function switchToState($stateName:String):void
		{
			if (_currentState == null)
			{
				_currentState=_stateDict[$stateName];
				_currentState.start();
				dispatchEvent(new Event(STATE_CHANGED_EVENT));
			}
			else
			{
				if (_currentState == _stateDict[$stateName])
				{
					if (_currentState.isStart == false)
					{
						_currentState.start();
						dispatchEvent(new Event(STATE_CHANGED_EVENT));
					}
					else
					{
					}
				}
				else
				{
					_currentState.sleep();
					_currentState=_stateDict[$stateName];
					_currentState.start();
					dispatchEvent(new Event(STATE_CHANGED_EVENT));
				}
			}
		}

		/**
		 * 添加一个场景
		 * 注意：场景名不能重复、场景不可以重复添加
		 * @param $stateName 
		 * @param $state
		 *
		 */
		public function addState($stateName:String, $state:IContainer_SM):void
		{
			for (var i:int=0; i < _stateArr.length; i++)
			{
				if (_stateNameArr[i] == $stateName || _stateArr[i] == $state)
				{
					return;
				}
			}
			
			addChild(DisplayObject($state));
			_stateDict[$stateName]=$state;
			$state.stateName=$stateName;
			_stateNameArr.push($stateName);
			_stateArr.push($state);
			$state.initialize();
		}
		
		/**
		 * 移除一个场景
		 * @param $stateName
		 *
		 */
		public function removeState($stateName:String):void
		{
			if (_stateDict[$stateName] != null)
			{
				if (this.contains(_stateDict[$stateName]))
				{
					removeChild(_stateDict[$stateName]);
				}
			}
			
			var n:int=_stateNameArr.length;
			
			for (var i:int=0; i < n; i++)
			{
				if (_stateNameArr[i] == $stateName)
				{
					_stateNameArr.splice(i, 1);
					_stateArr.splice(i, 1);
				}
			}
			
			_stateDict[$stateName]=null;
		}

		/**
		 * 获取一个场景
		 * @param $stateName
		 * @return
		 *
		 */
		public function getState($stateName:String):IContainer_SM
		{
			return _stateDict[$stateName];
		}

		/**
		 * 获取当前场景名
		 * @return
		 *
		 */
		public function getCurrentStateName():String
		{
			return _currentState.stateName;
		}
		
		/**
		 * 场景开始运行、添加监听等
		 * 
		 */
		override public function start():void{
			super.start();
		}
		
		/**
		 * 场景休眠、移除监听、移除实例
		 * 
		 */
		override public function sleep():void
		{
			if(this._isStart==true){
				sleepAllState();
				removeAllState();
				super.sleep();
			}
		}

		/**
		 * 移除所有场景
		 * @param $stateName
		 *
		 */
		private function removeAllState():void
		{
			for (var i:int=0; i < _stateArr.length; i++)
			{
				if (_stateDict[_stateNameArr[i]] != null)
				{
					if (this.contains(_stateDict[_stateNameArr[i]]))
					{
						removeChild(_stateDict[_stateNameArr[i]]);
					}
					_stateDict[_stateNameArr[i]]=null;
				}

				_stateNameArr.splice(i, 1);
				_stateArr.splice(i, 1);
			}
		}

		/**
		 * 休眠所有场景
		 *
		 */
		private function sleepAllState():void
		{
			for (var i:int=0; i < _stateArr.length; i++)
			{
				IContainer_SM(_stateArr[i]).sleep();
			}
		}
	}
}